package com.scott.ch01.spring;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;

/**
 * @author 黄后林 开启基于注解的事务管理功能
 * @version 1.0
 * @note 父容器=Spring容器
 * @date 2021-11-22 22:38:33
 */

@EnableTransactionManagement
@ComponentScan(value = "com.scott.ch01")
@Configuration
public class SpringConfiguration {

    @Bean
    public DataSource dataSource() throws PropertyVetoException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setUser("root");
        dataSource.setPassword("123456");
        dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
        dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/oa?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC");
        return dataSource;
    }


    @Bean
    public JdbcTemplate jdbcTemplate(DataSource dataSource) throws PropertyVetoException{
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        return  jdbcTemplate;
    }

    /**
     * 方法描述: 把事务管理器注册到容器中
     * @author: 黄后林
     * @date: 2021/11/24 14:11
     * @param
     * @return
     */
    @Bean
    public PlatformTransactionManager transactionManager(DataSource dataSource) throws PropertyVetoException{
        PlatformTransactionManager transactionManager = new DataSourceTransactionManager(dataSource);
        return  transactionManager;
    }




}
